package cn.lsh.partitioner;

import cn.lsh.mapper.HotCommentMapper;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.StringTokenizer;

public class HotComment2Partitioner extends Partitioner<Text, IntWritable> {

	@Override
	public int getPartition(Text key, IntWritable value, int numReduceTasks) {
		if (HotCommentMapper.counter.equals(key)) {
			//第四个分区统计热评总条数和每条评论的总分词数
			return 3;
		} else if (0 == value.get()) {
			//词对应出现的文件的个数的数据放在5个分区
			return 4;
		} else {
			StringTokenizer sza = new StringTokenizer(key.toString(), "_");
			String id = "";
			//map输出的第一类数据有两种形态
			while (sza.hasMoreTokens()) {
				//分割后取最后一个，肯定是id
				id = sza.nextToken();
			}
			//文件id对剩余分区数取模，保证同一个文件的数据再同一分区
			return Integer.parseInt(id) % (numReduceTasks - 2);
		}
	}
}
